package ui.face
{

	import com.greensock.loading.ImageLoader;

	import common.loader.BaseLoader;
	import common.loader.LoaderManager;
	import common.loader.LoaderType;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	import globals.Global;

	import ui.component.Label;

	import util.ColorUtil;
	import util.DisplayUtil;

	public class FriendCell extends Sprite
	{
		private var _nameLabel:Label;
		private var _faceHead:Bitmap;
		private var imageLoader:BaseLoader;
		private var _bg:Bitmap;
		private var _isNull:Boolean = true;
		private var _bgItems:Array;

		public function FriendCell()
		{
			_bg = new Bitmap();
			this.addChild(_bg);
			_bgItems = [Global.assetsManager.getAssetsAsBitmapData("invite_bg_default"), //
				Global.assetsManager.getAssetsAsBitmapData("invite_bg_over"), //
				Global.assetsManager.getAssetsAsBitmapData("friend_bg_default"), //
				Global.assetsManager.getAssetsAsBitmapData("friend_bg_over")];
			_bg.bitmapData = _bgItems[0];
			/////////////////
			_faceHead = new Bitmap();
			_faceHead.x = 20;
			_faceHead.y = 24;
			this.addChild(_faceHead);

			_nameLabel = new Label(12, 0xFFFFFF, false, "left", true);
			_nameLabel.y = 71;
			this.addChild(_nameLabel);
			//////////////////////////
			this.addEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, onOutHandler);
			this.addEventListener(MouseEvent.CLICK, onClick);
		}

		private function onOverHandler(event:MouseEvent):void
		{
			if(_isNull)
			{
				_bg.bitmapData = _bgItems[1];
			}
			else
			{
				_bg.bitmapData = _bgItems[3];
			}
		}

		private function onOutHandler(event:MouseEvent):void
		{
			if(_isNull)
			{
				_bg.bitmapData = _bgItems[0];
			}
			else
			{
				_bg.bitmapData = _bgItems[2];
			}
		}

		private function onClick(event:MouseEvent):void
		{

		}

		public function update(image:String = "", name:String = ""):void
		{
			_isNull = false;
			_bg.bitmapData = _bgItems[2];
			LoaderManager.add(image, LoaderType.IMAGE, imageHandler);
			_nameLabel.text = name;
			_nameLabel.x = (_bg.width - _nameLabel.width) * 0.5;
		}

		private function imageHandler(data:BitmapData):void
		{
			_faceHead.bitmapData = data;
		}

		public function clear():void
		{
			_isNull = true;
			_bg.bitmapData = _bgItems[0];
			_nameLabel.text = '';
			_faceHead.bitmapData = null;
		}
	}
}